package com.prs.crm.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.Account;
import com.prs.crm.service.AccountService;

public class AccountAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8408472108165081347L;
	
	private AccountService accountService;
	
	private Account account;
	
	private PaginationSupport pagination;
	
	private TreeNode root;
	
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public AccountService getAccountService() {
		return accountService;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public Account getAccount() {
		return account;
	}
	
	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	@Action("listaccount")
	public String list()
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Account.class);
		this.setPagination(this.getAccountService().listByPage(criteria,
							getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editaccount")
	public String execute()
	{
		return SUCCESS;
	}
	
	@Action(value="saveaccount",results={@Result(name="success",type="redirect",location="listaccount")})
	public String save()
	{
		this.getAccountService().save(account);
		return SUCCESS;
	}
	@Action("tree")
	public String tree()
	{
		return SUCCESS;
	}
	
	@Action(value="loadtree1",results={@Result(name="success",type="json",params={"root","root"})})
	public String loadtree()
	{
		root = new TreeNode();//注意ROOTNODE只是个虚拟节点，不做显示
		root.setId("0");
		
		TreeNode firstnode = new TreeNode();
		firstnode.setId("1");
		firstnode.setText("文件类型管理");
		
		List<TreeNode> childNodes = new ArrayList<TreeNode>();
		TreeNode childNode1 = new TreeNode();
		childNode1.setId("2");
		childNode1.setText("childnode1");
		
		//childnode1's branch
		TreeNode branch1= new TreeNode();
		branch1.setId("3");
		branch1.setText("branch1");
		List<TreeNode> branches = new ArrayList<TreeNode>();
		branches.add(branch1);
		childNode1.setItem(branches);
		
		TreeNode childNode2 = new TreeNode();
		childNode2.setId("4");
		childNode2.setText("childnode2");
		
		childNodes.add(childNode1);
		childNodes.add(childNode2);
		
		firstnode.setItem(childNodes);
		
		root.setItem(new ArrayList<TreeNode>());
		root.getItem().add(firstnode);
		return SUCCESS;
	}

}
